package com.example.cloud.config.conversion;

import org.beetl.sql.core.DefaultNameConversion;
import org.beetl.sql.core.annotatoin.Table;
import org.beetl.sql.core.kit.StringKit;
import org.springframework.context.annotation.Configuration;

import javax.persistence.Entity;

/**
 * Created by wangtao on 2018/4/24.
 */
@Configuration
public class MyNameConversion extends DefaultNameConversion {
    @Override
    public String getTableName(Class<?> c) {
        //@Entity拥有最高优先级
        Entity entity = (Entity)c.getAnnotation(Entity.class);
        if(entity != null){
            return entity.name();
        }
        Table table = (Table)c.getAnnotation(Table.class);
        if(table!=null){
            return table.name();
        }
        String tableName = StringKit.enCodeUnderlined(c.getSimpleName());
        return tableName;
    }

    @Override
    public String getColName(Class<?> c, String attrName) {
        return super.getColName(c, StringKit.enCodeUnderlined(attrName));
    }

    @Override
    public String getPropertyName(Class<?> c, String colName) {
        colName = StringKit.deCodeUnderlined(colName);
        return colName;
    }

    @Override
    public String getClassName(String tableName) {
        return super.getClassName(tableName);
    }

    @Override
    public String getColName(String attrName) {
        return super.getColName(attrName);
    }

    @Override
    public String getPropertyName(String colName) {
        return super.getPropertyName(colName);
    }
}
